package com.web1.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web1.utils.ConfigUtils;
import com.web1.utils.CookiesGetUtil;
import com.web1.utils.HttpUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest httpRequest,
                                HttpServletResponse httpResponse, Object arg2, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                           Object arg2, ModelAndView arg3) throws Exception {


    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object object) throws Exception {
        String urlString = request.getRequestURI();

        if (StringUtils.isEmpty(CookiesGetUtil.getCookies("token", request))) {
            String action = request.getParameter("code");
            if (StringUtils.isEmpty(action)) {
                StringBuilder url = new StringBuilder(ConfigUtils.getProperty("sso.url.checkLogin"));
                url.append("?returnUrl=http://web1.com").append(urlString);
                response.addHeader("location", url.toString());
                response.setStatus(302);
                return false;
            }
            if ("0".equals(request.getParameter("code"))) {
                String token = request.getParameter("token");
                boolean result = this.checkToken(request.getParameter("token"));
                if (result) {
                    Cookie clientCookie = new Cookie("token", token);
                    clientCookie.setMaxAge(60 * 30);
                    clientCookie.setPath("/");
                    response.addCookie(clientCookie);
                    return true;
                } else {
                    request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
                    return false;
                }
            }
            if ("1".equals(request.getParameter("code"))) {
                request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
                return false;
            }
            return true;
        } else {
            boolean result = this.checkToken(CookiesGetUtil.getCookies("token", request));
            if (result) {
                return true;
            } else {
                request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
                return false;
            }
        }
    }

    private boolean checkToken(String token) {
        Map<String, String> params = new HashMap<>();
        params.put("token", token);
        String result = null;
        try {
            result = HttpUtil.send(ConfigUtils.getProperty("sso.url.checkToken"), params, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject obj = JSON.parseObject(result);
        Integer code = obj.getInteger("code");
        if (code == 200) {
            return true;
        } else {
            return false;
        }
    }
}